<?php

namespace AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
use AdminBundle\Utility\Constant;

class BaseDeviceType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', TextType::class, [
            'label' => '名称标识',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入名称标识',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => false,
        ])->add('note', TextareaType::class, [
            'label' => '注释',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入注释',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => false,
            'empty_data' => '',
        ])->add('isEnabled', CheckboxType::class, array(
                'label' => '启用',
                'label_attr' => Constant::LABEL_ATTR_DEFAULT,
                'attr' => [
                    'class' => 'ace ace-switch ace-switch-5',],
                'required' => false,
            )
        )->add('imei', TextType::class, [
            'label' => 'IMEI',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入IMEI标识',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => true,
        ])->add('manufacturer', TextType::class, [
            'label' => '制造商',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入制造商',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => false,
            'empty_data' => '',
        ])->add('model', TextType::class, [
            'label' => '型号',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入设备型号',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => false,
            'empty_data' => '',
        ])->add('os', TextType::class, [
            'label' => '操作系统',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入操作系统',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => false,
            'empty_data' => '',
        ])->add('osVersion', TextType::class, [
            'label' => '系统版本',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入系统版本',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => false,
            'empty_data' => '',
        ])->add('macId', TextType::class, [
            'label' => 'MAC',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请输入MAC标识',
                'class' => Constant::INPUT_ATTR_CLASS],
            'required' => false,
            'empty_data' => '',
        ])->add('groupId', EntityType::class, [
            'label' => '终端组',
            'label_attr' => Constant::LABEL_ATTR_DEFAULT,
            'attr' => [
                'placeholder' => '请选择终端组',
                'class' => "chosen-select form-control"],
            'required' => false,
            'empty_data' => "",
            // input=choice|select , must be require
            'em' => 'default',
            'class' => 'AdminBundle:BaseGroup',
            'query_builder' => function (EntityRepository $repo) {
                return $repo->createQueryBuilder('u')->where('u.isEnabled=1')->orderBy('u.id', 'desc');
            },
            'choice_label' => function ($baseDataSource) {
                return $baseDataSource->getName() . '--' . $baseDataSource->getNote();
            },
            'choice_value' => 'id',
            'choice_name' => 'id',
            'expanded' => false,
            'multiple' => false,
        ]);

//        $builder->add('name')->add('imei')->add('note')->add('isEnabled')->add('creator')->add('ip')->add('ctime')->add('type')->add('model')->add('os')->add('osVersion')->add('macId')->add('groupId');
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AdminBundle\Entity\BaseDevice'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'adminbundle_basedevice';
    }


}
